package com.czn.service.impl;

import com.czn.dao.IllnessDao;
import com.czn.entity.Illness;
import com.czn.service.IIllnessService;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.beetl.sql.core.query.LambdaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author ChenZeNan
 * @create 2021-04-22 22:55
 */
@Service
public class IllnessServiceImpl implements IIllnessService {

    @Autowired
    private IllnessDao illnessDao;

    /**
     * 分页/模糊查询疾病
     *
     * @param pageNo
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public PageQuery<Illness> findIllnessList(Integer pageNo, Integer pageSize, String name) {
        LambdaQuery<Illness> query = illnessDao.createLambdaQuery();
        if (!StringUtils.isEmpty(name)) {
            query.andLike(Illness::getName, "%" + name + "%");
        }
        if (pageNo > 0 && pageSize > 0) {
            PageQuery<Illness> page = query.desc(Illness::getId).page(pageNo, pageSize);
            return page;
        }
        return null;
    }

    /**
     * 新增疾病
     *
     * @param illness
     * @return
     */
    @Override
    public ResponseResult addIllness(Illness illness) {
        ResponseResult result = new ResponseResult();
        // 根据身份证查询数据库中是否已有该对象了
        LambdaQuery<Illness> query = illnessDao.createLambdaQuery();
        query.andEq(Illness::getName, illness.getName());
        Illness sysIllness = query.single();
        if (sysIllness == null) { // 数据库中没有该对象，直接插入
            illnessDao.insert(illness);
            result.setCode("300");
            result.setMessage("信息添加成功！");
            return result;
        }
        // 数据库中已有该对象了
        result.setCode("301");
        result.setMessage("该疾病信息已存在数据库中~");
        return result;
    }

    /**
     * 修改疾病信息
     *
     * @param illness
     * @return
     */
    @Override
    public ResponseResult editIllness(Illness illness) {
        ResponseResult result = new ResponseResult();
        int rows = illnessDao.updateById(illness);
        if (rows > 0) {
            result.setCode("500");
            result.setMessage("信息更新成功！");
            return result;
        }
        result.setCode("501");
        result.setMessage("信息更新失败！");
        return result;
    }

    /**
     * 根据疾病id查询疾病
     *
     * @param id
     * @return
     */
    @Override
    public Illness findIllnessById(Integer id) {
        Illness illness = illnessDao.single(id);
        return illness;
    }

    /**
     * 删除疾病
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteIllnessById(Integer id) {
        ResponseResult result = new ResponseResult();
        int rows = illnessDao.deleteById(id);
        if (rows > 0) {
            result.setCode("400");
            result.setMessage("删除成功！");
            return result;
        }
        result.setCode("401");
        result.setMessage("删除失败！");
        return result;
    }
}
